package com.ruoyi.project.formula.formula.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.formula.formula.domain.Formula;
import com.ruoyi.project.formula.formula.service.IFormulaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公式编辑Controller
 *
 * @author shixiangwen
 * @date 2020-10-11
 */
@Controller
@RequestMapping("/formula/formula")
public class FormulaController extends BaseController {
    private String prefix = "formula/formula";

    @Autowired
    private IFormulaService formulaService;

    @RequiresPermissions("formula:formula:view")
    @GetMapping()
    public String formula() {
        return prefix + "/formula";
    }

    /**
     * 查询公式编辑列表
     */
    @RequiresPermissions("formula:formula:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Formula formula) {
        startPage();
        List<Formula> list = formulaService.selectFormulaList(formula);
        return getDataTable(list);
    }

    /**
     * 导出公式编辑列表
     */
    @RequiresPermissions("formula:formula:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Formula formula) {
        List<Formula> list = formulaService.selectFormulaList(formula);
        ExcelUtil<Formula> util = new ExcelUtil<Formula>(Formula.class);
        return util.exportExcel(list, "formula");
    }

    /**
     * 新增公式编辑
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存公式编辑
     */
    @RequiresPermissions("formula:formula:add")
    @Log(title = "公式编辑", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Formula formula) {
        return toAjax(formulaService.insertFormula(formula));
    }

    /**
     * 修改公式编辑
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Formula formula = formulaService.selectFormulaById(id);
        mmap.put("formula", formula);
        return prefix + "/edit";
    }

    /**
     * 预览公式
     */
    @GetMapping("/preview/{id}")
    public String preview(@PathVariable("id") Long id, ModelMap mmap) {
        Formula formula = formulaService.selectFormulaById(id);
        mmap.put("formula", formula);
        return prefix + "/preview";
    }

    /**
     * 修改保存公式编辑
     */
    @RequiresPermissions("formula:formula:edit")
    @Log(title = "公式编辑", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Formula formula) {
        return toAjax(formulaService.updateFormula(formula));
    }

    /**
     * 删除公式编辑
     */
    @RequiresPermissions("formula:formula:remove")
    @Log(title = "公式编辑", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(formulaService.deleteFormulaByIds(ids));
    }
}
